package com.aaa.marketresearch.webclient.gwt.admin.widget;

import com.aaa.marketresearch.webclient.gwt.admin.AdminRequester;
import com.aaa.marketresearch.webclient.gwt.domain.BillContent;
import com.aaa.marketresearch.webclient.gwt.util.Responder;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;

/**
 * @author Nail
 */
public class BillTable extends FlowPanel {

    private static BillTable instance = null;

    public static BillTable getInstance() {
        if (instance == null) {
            instance = new BillTable();
        }
        return instance;
    }

    public BillTable() {
        super();
    }

    public void loadData() {
        AdminRequester request = new AdminRequester();
        request.addResponder(new Responder() {
            @Override
            public void handleGetResponse(String content) {
                BillTable.this.goodsUpdated(content);
            }
        });
        request.getBillContents();
    }

    private void goodsUpdated(String content) {
        this.clear();
        JSONArray data = (JSONArray) JSONParser.parseLenient(content);
        int n = data.size();

        Label column;

        FlowPanel div = new FlowPanel();

        column = new Label("покупатель");
        column.setStyleName("billContentMainLabel-0");
        column.addStyleName("billContentTitle");
        div.add(column);

        column = new Label("соц. статус");
        column.setStyleName("billContentMainLabel-1");
        column.addStyleName("billContentTitle");
        div.add(column);

        column = new Label("магазин");
        column.setStyleName("billContentMainLabel-2");
        column.addStyleName("billContentTitle");
        div.add(column);

        column = new Label("штрихкод");
        column.setStyleName("billContentMainLabel-3");
        column.addStyleName("billContentTitle");
        div.add(column);

        column = new Label("название товара");
        column.setStyleName("billContentMainLabel-4");
        column.addStyleName("billContentTitle");
        div.add(column);

        column = new Label("кол./вес/объем");
        column.setStyleName("billContentMainLabel-5");
        column.addStyleName("billContentTitle");
        div.add(column);

        column = new Label("стоимость");
        column.setStyleName("billContentMainLabel-6");
        column.addStyleName("billContentTitle");
        div.add(column);

        this.add(div);

        for (int i = 0; i < n; i++) {
            JSONObject obj = data.get(i).isObject();
            BillContent billContent = new BillContent(obj);
            BillContentView billContentView = new BillContentView(billContent);
            this.add(billContentView);
        }
    }

}
